package top.linyuxb.lycloud.rpc.starter.common;

import cn.hutool.core.map.MapUtil;
import lombok.extern.slf4j.Slf4j;
import top.linyuxb.lycloud.rpc.starter.dto.LyRpcReferenceDto;
import top.linyuxb.lycloud.rpc.starter.dto.LyRpcRibbonDto;

import java.util.Map;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/6/6 17:35
 * @Desc: rpc全局数据透传
 */
@Slf4j
public class LyRpcData {
    /**
     * 存放rpc远程调用版本信息
     */
    private static final Map<String, LyRpcReferenceDto> RPC_REFERENCE_MAP = MapUtil.newHashMap();
    /**
     * 存放rpc远程调用负载均衡信息
     */
    private static final Map<String, LyRpcRibbonDto> RPC_RIBBON_MAP = MapUtil.newHashMap();

    /**
     * 放入版本
     *
     * @param key
     * @param lyRpcReferenceDto
     */
    public static void putLyRpcReference(String key, LyRpcReferenceDto lyRpcReferenceDto) {
        log.debug("LyRpcData putVersion key:{},version:{}", key, lyRpcReferenceDto);
        RPC_REFERENCE_MAP.put(key, lyRpcReferenceDto);
    }

    /**
     * 获取版本
     *
     * @param key
     * @return
     */
    public static LyRpcReferenceDto getLyRpcReference(String key) {
        LyRpcReferenceDto lyRpcReferenceDto = RPC_REFERENCE_MAP.get(key);
        log.debug("LyRpcData getVersion key:{},lyRpcReferenceDto:{}", key, lyRpcReferenceDto);
        return lyRpcReferenceDto;
    }

    /**
     * 放入负载均衡器
     *
     * @param key 类名称_方法名称
     * @param rpcRibbonDto
     */
    public static void putRibbon(String key, LyRpcRibbonDto rpcRibbonDto) {
        log.debug("LyRpcData putRibbon key:{},rpcRibbonDto:{}", key, rpcRibbonDto);
        RPC_RIBBON_MAP.put(key, rpcRibbonDto);
    }

    /**
     * 获取负载均衡器
     *
     * @param key
     * @return
     */
    public static LyRpcRibbonDto getRibbon(String key) {
        LyRpcRibbonDto rpcRibbonDto = RPC_RIBBON_MAP.getOrDefault(key, null);
        log.debug("LyRpcData getRibbon key:{},rpcRibbonDto:{}", key, rpcRibbonDto);
        return rpcRibbonDto;
    }
}
